require(["common", "jQuery", "coAjax", "queryString", "href", "hash_routie"], function(jSouper, $, coAjax, QueryString, href, hash_routie) {

	//获取购买二进制商品的用户列表
	function _init_bina_user() {
		coAjax.get(appConfig.bus.extend.bina.user, function(result) {
			App.set("$Cache.bina.user_list", result.result);
		});
	};
	//获取二进制商品信息
	function _init_bina_goods() {
		coAjax.get(appConfig.bus.extend.bina.goods, function(result) {
			App.set("$Cache.bina.bina_list", result.result);
		});
	};
	hash_routie.on("bina_user", function() {
		coAjax.get(appConfig.bus.loginer, function(result) {
			App.set("bus_loginer", result.result); //重新获取永久分销人员的名单。
		});
		_init_bina_goods();
		_init_bina_user();
	});

	//兑换确认打款
	App.set("$Event.bina_申请兑换中_to_商家已付款", function(e, vm) {
		coAjax.put(appConfig.bus.extend.bina.申请兑换中_to_商家已付款, {
			user_id: vm.get("user_id")
		}, function(result) {
			vm.set(result.result);
		});
	});

	//处理回调的bina_list数据
	function _format_editing_user_bina_list(result) {
		var bina_list = result.result;
		App.set("$Cache.bina_user.editing.bina_list", bina_list);
		//bina_id_list
		var bina_id_list = jSouper.map(bina_list, function(item) {
			return item.bina._id
		})
		App.set("$Cache.bina_user.editing.bina_id_list", bina_id_list);

		//更新大列表的数据
		jSouper.forEach(App.get("$Cache.bina.bina_list"), function(bina_info) {
			var _id_index = jSouper.indexOf(bina_id_list, bina_info.bina._id);
			if (_id_index !== -1) {
				bina_info.bina = bina_list[_id_index].bina;
			}
		});
		App.model.touchOff("$Cache.bina.bina_list");
	}
	//编辑 用户的分销商品列表
	App.set("$Event.bina_user.edit_bina_list", function(e, vm) {
		App.set("$Cache.bina_user.editing.user_bina", vm.get());
		coAjax.get(appConfig.bus.extend.bina.get_bina_list_of_user + vm.get("user._id"), _format_editing_user_bina_list);
	});
	App.set("$Event.bina_user.close_edit", function() {
		App.set("$Cache.bina_user.editing.user_bina", null);
	});
	//添加用户到分销人员列表
	App.set("$Event.bina_user.add_user_to_bina", function(e, vm) {
		var bina_id = vm.get("bina._id");
		var user_id = App.get("$Cache.bina_user.editing.user_bina.user._id")
		coAjax.post(appConfig.bus.extend.bina.add_user_to_bina + bina_id, {
			user_id: user_id,
			return_type: "bina_list"
		}, _format_editing_user_bina_list);
	});
	//解锁用户某个商品的分销权限
	App.set("$Event.bina_user.unlock_user_from_bina", function(e, vm) {
		var self = this;
		self.setAttribute("disabled", true);
		var bina_id = vm.get("bina._id");
		var user_id = App.get("$Cache.bina_user.editing.user_bina.user._id")
		coAjax.put(appConfig.bus.extend.bina.unlock_user, {
			user_id: user_id,
			bina_id: bina_id,
			return_type: "bina_list"
		}, function(data) {
			self.removeAttribute("disabled");
			_format_editing_user_bina_list(data);
			alert("success", "成功冻结分销权限")
		});
	});
	//锁定用户某个商品的分销权限
	App.set("$Event.bina_user.lock_user_from_bina", function(e, vm) {
		var self = this;
		self.setAttribute("disabled", true);
		var bina_id = vm.get("bina._id");
		var user_id = App.get("$Cache.bina_user.editing.user_bina.user._id")
		coAjax.put(appConfig.bus.extend.bina.lock_user, {
			user_id: user_id,
			bina_id: bina_id,
			return_type: "bina_list"
		}, function(data) {
			self.removeAttribute("disabled");
			_format_editing_user_bina_list(data);
			alert("success", "成功解冻分销权限")
		});
	});
	//批量处理
	App.set("$Event.bina_user.add_user_to_bina_list", function(e, vm) {
		var bina_id_list = [];
		var user_bina_id_list = App.get("$Cache.bina_user.editing.bina_id_list");
		jSouper.forEach(App.get("$Cache.bina.bina_list"), function(item) {
			if (user_bina_id_list.indexOf(item.bina._id) === -1) {
				bina_id_list.push(item.bina._id);
			}
		});
		if (bina_id_list.length == 0) {
			alert("用户已经拥有全部分销商品的分销权限，无需重复添加");
			return
		}
		var user_id = App.get("$Cache.bina_user.editing.user_bina.user._id")
		coAjax.post(appConfig.bus.extend.bina.add_user_to_bina_list, {
			user_id: user_id,
			bina_id_list: bina_id_list,
			return_type: "bina_list"
		}, _format_editing_user_bina_list);
	});
	//添加到永久分销人员列表
	App.set("$Event.bina_user.add_user_to_forever_bina_user_list", function() {
		var self = this;
		self.setAttribute("disabled", true);
		coAjax.post(appConfig.bus.extend.bina.add_user_to_forever_bina_user_list, {
			user_id: App.get("$Cache.bina_user.editing.user_bina.user._id")
		}, function(result) {
			self.removeAttribute("disabled");
			App.set("bus_loginer", result.result);
			alert("success", "用户已经添加到永久分销人员列表中");
		});
	});
	//移除永久分销人员
	App.set("$Event.bina_user.remove_user_form_forever_bina_user_list", function() {
		var self = this;
		self.setAttribute("disabled", true);
		coAjax["delete"](appConfig.bus.extend.bina.remove_user_form_forever_bina_user_list, {
			user_id: App.get("$Cache.bina_user.editing.user_bina.user._id")
		}, function(result) {
			self.removeAttribute("disabled");
			App.set("bus_loginer", result.result)
			alert("success", "用户已从永久分销人员列表中移除");
		});
	});
	App.set("$Event.bina_user.add_user_to_all_bina", function() {
		var bina_id_list = jSouper.map(App.get("$Cache.bina.bina_list"), function(item) {
			return item.bina._id;
		});
		var user_maybe_info = App.get("$Cache.bina_user.user_maybe_info_to_add");
		coAjax.post(appConfig.bus.extend.bina.add_user_to_bina_list, {
			user_maybe_info: user_maybe_info,
			bina_id_list: bina_id_list,
			return_type: "user_list"
		}, function(result) {
			alert("success", "用户" + user_maybe_info + "权限添加完成，享有所有分销商品的分销权限");
			App.set("$Cache.bina.user_list", result.result);
			App.set("$Cache.bina_user.editing", {});
		});
	});
});